package com.design.twords.adatper;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.design.twords.R;
import com.design.twords.activity.WordListActivity;
import com.design.twords.config.ConfigData;
import com.design.twords.config.ConstantValue;
import com.design.twords.database.BookList;
import com.design.twords.database.Phrase_all;
import com.design.twords.database.Phrase_learn;
import com.design.twords.database.RelWord_all;
import com.design.twords.database.RelWord_learn;
import com.design.twords.database.Sentence_all;
import com.design.twords.database.Sentence_learn;
import com.design.twords.database.Translation_all;
import com.design.twords.database.Translation_learn;
import com.design.twords.database.UserConfig;
import com.design.twords.database.Word_all;
import com.design.twords.database.Word_learn;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Create by Alen
 * Time:  13:59
 */
public class FloderListAdapter extends RecyclerView.Adapter<FloderListAdapter.MyViewHolder> {

    private static final String TAG = "FloderListAdapter";

    public interface ItemClickListener{
        void itemLongClickListener(int position);
    }

    private LayoutInflater myInflater;
    private Context context;
    private ArrayList<BookList> bookLists;
    private int viewId;

    private ItemClickListener listener;
    private SharedPreferences preferences;


    public FloderListAdapter(Context context, ArrayList<BookList> bookLists, int viewId) {
        this.myInflater = LayoutInflater.from(context);
        this.context = context;
        this.bookLists = bookLists;
        this.viewId = viewId;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FloderListAdapter.MyViewHolder(myInflater.inflate(viewId,parent,false));
    }

    public void setBookLists(ArrayList<BookList> bookLists) {
        this.bookLists = bookLists;
    }

    public ItemClickListener getListener() {
        return listener;
    }

    public void setListener(ItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        BookList bookList = bookLists.get(position);
        holder.floder_name.setText(bookList.getBookname());
        holder.floder_creattime.setText(bookList.getCreatetime());

        holder.floder_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WordListActivity.class);

                intent.putExtra("floder_id", bookList.getBookId());
                intent.putExtra("sources", "floder");
                context.startActivity(intent);

            }
        });

        holder.floder_detail.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(listener != null){
                    listener.itemLongClickListener(position);
                }
                return true;
            }
        });

    }

    @Override
    public int getItemCount() {
        return bookLists.size();
    }

    public void deleteItem(int position) {
        BookList itemWordBook = bookLists.get(position);
        String type = ConstantValue.getTypeById(itemWordBook.getBookId());


        int bookList_before = LitePal.count(BookList.class);
        Log.i(TAG, "booksize" + bookList_before);
        //如果当前书籍为正在使用的书籍 同时删除计划 和使用的数据
        List<UserConfig> all = LitePal.where("userId = ?", ConfigData.getUserId()).find(UserConfig.class);


        if(itemWordBook.getBookId() == all.get(0).getCurrentBookId()){
            LitePal.deleteAll(UserConfig.class,"userId = ?", ConfigData.getUserId());

            Log.d(TAG,"delete UserConfig");

            LitePal.beginTransaction();
            try{
                LitePal.deleteAll(Word_learn.class);
                LitePal.deleteAll(Translation_learn.class);
                LitePal.deleteAll(Sentence_learn.class);
                LitePal.deleteAll(RelWord_learn.class);
                LitePal.deleteAll(Phrase_learn.class);
                LitePal.setTransactionSuccessful();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                LitePal.endTransaction();
            }
        }

        LitePal.beginTransaction();
        try{
            LitePal.deleteAll(BookList.class, "bookId = ?", itemWordBook.getBookId()+"");
            LitePal.deleteAll(Word_all.class, "wordBook = ?", type);
            LitePal.deleteAll(Translation_all.class, "wordBook = ?", type);
            LitePal.deleteAll(RelWord_all.class, "wordBook = ?", type);
            LitePal.deleteAll(Phrase_all.class, "wordBook = ?", type);
            LitePal.deleteAll(Sentence_all.class, "wordBook = ?", type);
            bookLists.remove(position);
            LitePal.setTransactionSuccessful();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            LitePal.endTransaction();
            notifyDataSetChanged();
        }
        int bookList_last = LitePal.count(BookList.class);
        Log.i(TAG, "booksize" + bookList_last);

    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView floder_name, floder_creattime;
        private RelativeLayout floder_detail;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            floder_name = itemView.findViewById(R.id.floder_name);
            floder_creattime = itemView.findViewById(R.id.floder_creattime);
            floder_detail = itemView.findViewById(R.id.floder_detail);
        }
    }
}
